var MenuService = function () {

    var menuService = new Object();

    menuService.saveMenu = function (updateType, $form, successCallback) {
        clearFormValidateMsg($form);
        var pass = validateItemNotNull($form.find("#cModelName"));
        if (!pass)
            return;

        var requestData = $form.serialize();
        var title = "";
        var text = "";
        if (updateType == "NEW.TOP") {
            requestData = requestData + "&updateType=NEW.TOP";
            title = "确定添加?";
            text = "是否确定添加新的菜单!";
        }
        else if (updateType == "NEW.CHILD") {
            requestData = requestData + "&updateType=NEW.CHILD";
            title = "确定添加?";
            text = "是否确定添加新的菜单!";
        }
        else if (updateType == "EDIT") {
            requestData = requestData + "&updateType=UPDATE";
            title = "确定修改?";
            text = "是否确定修改此菜单!";
        }
        else
            return;

        swal({
            title: title,
            text: text,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (result.value) {
                saveWebMenu(requestData, function (data) {
                    successCallback(updateType, data);
                });
            }
        });
    };

    function getContextPath() {
        var pathName = document.location.pathname;
        var index = pathName.substr(1).indexOf("/");
        var result = pathName.substr(0, index + 1);
        return result;
    }

    function saveWebMenu(requestData, successCallback) {
        $.ajax({
            type: 'POST',
            data: requestData,
            url: getContextPath() + '/modelmanage/save',
            success: function (data) {
                //成功之后
                if (data.success) {
                    if (successCallback != null)
                        successCallback(data.data);
                }
                else {
                    swal("错误", data.errorMsg, "error");
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    };

    menuService.deleteWebMenu = function (cModelID, successCallback) {
        if (cModelID == null)
            return;

        swal({
            title: "确定删除?",
            text: "确定删除当前菜单及其子菜单,删除后无法撤销",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (result.value) {
                deleteWebMenu(cModelID, successCallback);
            }
        });
    };


    menuService.syncPermission = function (successCallback) {
        $.ajax({
            type: 'POST',
            url: getContextPath() + '/modelmanage/syncPermission/',
            success: function (data) {
                //成功之后
                if (data.success) {
                    if (successCallback != null)
                        successCallback();
                }
                else {
                    swal("错误", data.errorMsg, "error");
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    };

    function deleteWebMenu(cModelID, successCallback) {
        $.ajax({
            type: 'DELETE',
            url: getContextPath() + '/modelmanage/delete/' + cModelID,
            success: function (data) {
                //成功之后
                if (data.success) {
                    if (successCallback != null)
                        successCallback();
                }
                else {
                    swal("错误", data.errorMsg, "error");
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    };

    menuService.loadMenuDetail = function (menuxh, successCallback, errorCallback) {
        $.ajax({
            type: 'POST',
            url: getContextPath() + '/modelmanage/menudata/' + menuxh,
            success: function (data) {
                if (data.success) {
                    if (successCallback != null) {
                        successCallback(data.data);
                    }
                }
                else {
                    if (errorCallback != null) {
                        errorCallback(data.errorMsg);
                    }
                }
            },
            error: function (xhr, type) {
                if (errorCallback != null) {
                    errorCallback("系统发生内部错误!请稍后再试!");
                }
            }
        });
    }


    return menuService;
}